'''
Author: LostPatrol_3
Date: 2023-11-04 21:44:36
LastEditors: LostPatrol_3
LastEditTime: 2023-11-04 21:53:58
Description: 
'''
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QTextEdit, QFileDialog
from PyQt5.QtGui import QTextImageFormat
from PyQt5.QtGui import QTextCursor

class ImageDisplayWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.message_edit = QTextEdit(self)
        self.message_edit.setText('124819274981724891724\n1927498124789')
        self.select_image_button = QPushButton('Select Image', self)
        self.select_image_button.clicked.connect(self.selectImage)

        layout = QVBoxLayout()
        layout.addWidget(self.message_edit)
        layout.addWidget(self.select_image_button)
        self.setLayout(layout)

        self.setWindowTitle('Image Display in QTextEdit')
        self.show()

    def selectImage(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "Select Image", "", "Images (*.png *.jpg *.bmp);;All Files (*)", options=options)
        if file_name:
            self.displayImage(file_name)

    def displayImage(self, image_path):
        # document = self.message_edit.document()
        cursor = self.message_edit.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText('\n')
        cursor.movePosition(QTextCursor.End)
        image_format = QTextImageFormat()
        image_format.setWidth(100)
        image_format.setHeight(100)
        image_format.setName(image_path)
        cursor.insertImage(image_format)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageDisplayWindow()
    sys.exit(app.exec_())
